﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CardInChina.Bussiness.Frontend.Models;

namespace CardInChina.Web.Mvc
{
    /// <summary>
    /// Represent a website end-user of the current session
    /// </summary>
    public class User
    {
        // 是否登录
        public bool IsLogon
        {
            get { return Details != null; }
        }

        // 推荐人ID
        public int? Recommended
        {
            get { return HttpContext.Current.Session["$Recommended$"] as int?; }
            set { HttpContext.Current.Session["$Recommended$"] = value; }
        }

        // Referrer
        public string Referrer
        {
            get { return HttpContext.Current.Session["$Referrer$"] as string; }
            set { HttpContext.Current.Session["$Referrer$"] = value; }
        }

        // Referer
        public string HipCode
        {
            get { return HttpContext.Current.Session["$HipCode$"] as string; }
            set { HttpContext.Current.Session["$HipCode$"] = value; }
        }

        // Get -> 如果已经登录，或者详细信息
        // Set -> 保存用户详细信息
        public CustomerInfo Details
        {
            get { return HttpContext.Current.Session["$User$"] as CustomerInfo; }
            set { HttpContext.Current.Session["$User$"] = value; }
        }

        public void TrySaveReferrer(HttpRequestBase request)
        {
            var referrer = request.UrlReferrer;
            var url = request.Url;
            if (referrer != null && url != null && referrer.Host != url.Host)
            {
                Referrer = referrer.ToString();
            }
        }

        public int[] LastOrders
        {
            get { return HttpContext.Current.Session["$LastOrders$"] as int[]; }
            set { HttpContext.Current.Session["$LastOrders$"] = value; }
        }

    }
}